package service

import (
	"gin/model"
	uuid "github.com/satori/go.uuid"
	teamones_helper "github.com/teamones-open/go-helper"
)

// 生成密钥对
func GenerateToken() (accessKey string, secretKey string, err error) {
	// 生成token
	authUUID := uuid.NewV4().String()
	var authModel model.Auth
	authModel.AccessKey = teamones_helper.GenerateMd5(authUUID + "_access_key")
	authModel.SecretKey = teamones_helper.GenerateMd5(authUUID + "_secret_key")

	accessKey, secretKey, err = authModel.Generate()

	return
}

// 获取密钥对
func GetToken() (accessKey string, secretKey string, err error) {
	var authModel model.Auth
	accessKey, secretKey, err = authModel.GetToken()
	return
}

// 验证token是否正确
func CheckToken(token string) (check bool) {
	accessKey, secretKey, err := GetToken()
	if err != nil {
		return false
	} else {
		md5 := teamones_helper.GenerateMd5(accessKey + secretKey)
		if token != md5 {
			return false
		}
	}
	return true
}
